package com.taojin.shoppinginterest.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.taojin.shoppinginterest.R;
import com.taojin.shoppinginterest.bean.ShopList;
import com.taojin.shoppinginterest.utils.BitmapHelper;

import java.util.List;

/**
 * Created by T400 on 15-5-11.
 */
public class SelectionFragmentAdapter extends AppAdapter<ShopList> {
    private final int TYPE_1 = 0;
    private final int TYPE_2 = 1;
    private BitmapUtils bitmapUtils;
    public SelectionFragmentAdapter(List<ShopList> list, Context context) {
        super(list, context);
        bitmapUtils= BitmapHelper.getInstance().getBitmapUtils(context, R.mipmap.icon,R.mipmap.icon);
    }

    @Override
    public View createItemView(int position, View convertView, ViewGroup parent) {
        ViewHolder1 vh1=null;
        ViewHolder2 vh2=null;
        int type=getItemViewType(position);
        Log.e("type",type+"");
        if(convertView==null){
            switch (type){
                case TYPE_1:
                    convertView=inflater.inflate(R.layout.item_selectionfragmentadapter,parent,false);
                    vh1=new ViewHolder1(convertView);
                    convertView.setTag(vh1);
                    break;
                case TYPE_2:
                    convertView=inflater.inflate(R.layout.item_selectionfragmentadapter2,parent,false);
                    vh2=new ViewHolder2(convertView);
                    convertView.setTag(vh2);
                    break;
            }
        }else{
            switch (type){
                case TYPE_1:
                    vh1= (ViewHolder1) convertView.getTag();
                    break;
                case TYPE_2:
                    vh2= (ViewHolder2) convertView.getTag();
                    break;
            }
        }
        switch (type){
            case TYPE_1:

                vh1.tvName.setText(list.get(position).getName());
                vh1.tvCate.setText(list.get(position).getCate());
                String[] imgList=list.get(position).getImg_list();
                bitmapUtils.display(vh1.imgLogo1, imgList[0]);
                bitmapUtils.display(vh1.imgLogo2, imgList[1]);
                bitmapUtils.display(vh1.imgLogo3, imgList[2]);
                vh1.tvTopic.setText(list.get(position).getTopic_info().getName());
                vh1.tvTag1.setText(list.get(position).getTag_list()[0]);
                vh1.tvTag2.setText(list.get(position).getTag_list()[1]);
                break;
            case TYPE_2:
                vh2.tvName.setText(list.get(position).getName());
                vh2.tvCate.setText(list.get(position).getCate());
                bitmapUtils.display(vh2.imgLogo1, list.get(position).getImg_list()[0]);
                bitmapUtils.display(vh2.imgLogo2, list.get(position).getImg_list()[1]);
                bitmapUtils.display(vh2.imgLogo3, list.get(position).getImg_list()[2]);
                vh2.tvTopic.setText(list.get(position).getTopic_info().getName());
                vh2.tvTag1.setText(list.get(position).getTag_list()[0]);
                vh2.tvTag2.setText(list.get(position).getTag_list()[1]);
                vh2.tvTag3.setText(list.get(position).getTag_list()[2]);
                break;


        }

        return convertView;
    }

    @Override
    public int getItemViewType(int position) {
        int size=list.get(position).getTag_list().length;
        switch (size){
            case 2:
              return TYPE_1;
            case 3:
                return TYPE_2;
        }
        return -1;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }
    public class ViewHolder1{
        @ViewInject(R.id.tvname_selecttionfragment_adapter)
        private TextView tvName;
        @ViewInject(R.id.tvcate_selecttionfragment_adapter)
        private TextView tvCate;
        @ViewInject(R.id.imagelogo1_selecttionfragment_adapter)
        private ImageView imgLogo1;
        @ViewInject(R.id.imagelogo2_selecttionfragment_adapter)
        private ImageView imgLogo2;
        @ViewInject(R.id.imagelogo3_selecttionfragment_adapter)
        private ImageView imgLogo3;
        @ViewInject(R.id.tv_topic_info_selectionfragmentadapter)
        private TextView tvTopic;
        @ViewInject(R.id.tv_tag1_selectionfragmentadapter)
        private TextView tvTag1;
        @ViewInject(R.id.tv_tag2_selectionfragmentadapter)
        private TextView tvTag2;

        public ViewHolder1(View convertView) {
            ViewUtils.inject(this,convertView);
        }
    }

    public class ViewHolder2{
        @ViewInject(R.id.tvname_selecttionfragment_adapter2)
        private TextView tvName;
        @ViewInject(R.id.tvcate_selecttionfragment_adapter2)
        private TextView tvCate;
        @ViewInject(R.id.imagelogo1_selecttionfragment_adapter2)
        private ImageView imgLogo1;
        @ViewInject(R.id.imagelogo2_selecttionfragment_adapter2)
        private ImageView imgLogo2;
        @ViewInject(R.id.imagelogo3_selecttionfragment_adapter2)
        private ImageView imgLogo3;
        @ViewInject(R.id.tv_topic_info_selectionfragmentadapter2)
        private TextView tvTopic;
        @ViewInject(R.id.tv_tag1_selectionfragmentadapter2)
        private TextView tvTag1;
        @ViewInject(R.id.tv_tag2_selectionfragmentadapter2)
        private TextView tvTag2;
        @ViewInject(R.id.tv_tag3_selectionfragmentadapter2)
        private TextView tvTag3;

        public ViewHolder2(View convertView) {
            ViewUtils.inject(this,convertView);
        }
    }
}
